/**
 * Copyright 2012 Wangxr, vintage.wang@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "AutoConfig.h"

AutoConfig::AutoConfig()
{
	m_TUXONE_APP_GATE_ADDR = "127.0.0.1:26000";
	m_TUXONE_APP_SERVER_LISTENER_ADDR = "0.0.0.0:0";
	m_TUXONE_APP_SERVER_CONCURRENTMAX = 512;
	m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS = 256;
	m_TUXONE_APP_SERVER_THREADMODE = MT;
	m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN = 3;
	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX = 128;
	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE = 10;
	m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS = 128;
	m_TUXONE_APP_HEART_INTERVAL = 3;
	m_TUXONE_APP_BUFFER_CACHEDSIZE = 1024000;
	m_TUXONE_APP_SOCKET_RWTIMEOUT = 90;
	m_TUXONE_APP_LOG_ENABLE = true;
	m_TUXONE_APP_LOG_LEVEL = 4;
	m_TUXONE_APP_LOG_PRINTWHERE = false;
	m_TUXONE_APP_LOG_PRINTFILE = true;
	m_TUXONE_APP_LOG_PRINTCONSOLE = false;

}

AutoConfig::~AutoConfig()
{
}

AutoConfig* AutoConfig::GetInstance()
{
	static AutoConfig *singleton = new AutoConfig();
	return singleton;
}

void AutoConfig::PrintConfig()
{
	const char* tmp = "";
	tmp = tmp;
	logger->info(LTRACE, "tuxone.app.gate.addr=%s", m_TUXONE_APP_GATE_ADDR.c_str());

	logger->info(LTRACE, "tuxone.app.server.listener.addr=%s", m_TUXONE_APP_SERVER_LISTENER_ADDR.c_str());

	logger->info(LTRACE, "tuxone.app.server.concurrentMax=%d", m_TUXONE_APP_SERVER_CONCURRENTMAX);

	logger->info(LTRACE, "tuxone.app.server.maxLongConnections=%d", m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS);

	if(m_TUXONE_APP_SERVER_THREADMODE == MT) tmp = "MT";
	if(m_TUXONE_APP_SERVER_THREADMODE == ST) tmp = "ST";
	logger->info(LTRACE, "tuxone.app.server.threadMode=%s", tmp);

	logger->info(LTRACE, "tuxone.app.server.threadPool.threadMin=%d", m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN);

	logger->info(LTRACE, "tuxone.app.server.threadPool.threadMax=%d", m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX);

	logger->info(LTRACE, "tuxone.app.server.threadPool.threadMaxIdle=%d", m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE);

	logger->info(LTRACE, "tuxone.app.client.cachedConnections=%d", m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS);

	logger->info(LTRACE, "tuxone.app.heart.interval=%d", m_TUXONE_APP_HEART_INTERVAL);

	logger->info(LTRACE, "tuxone.app.buffer.cachedSize=%d", m_TUXONE_APP_BUFFER_CACHEDSIZE);

	logger->info(LTRACE, "tuxone.app.socket.rwtimeout=%d", m_TUXONE_APP_SOCKET_RWTIMEOUT);

	logger->info(LTRACE, "tuxone.app.log.enable=%s", m_TUXONE_APP_LOG_ENABLE ? "true" : "false");

	logger->info(LTRACE, "tuxone.app.log.level=%d", m_TUXONE_APP_LOG_LEVEL);

	logger->info(LTRACE, "tuxone.app.log.printFile=%s", m_TUXONE_APP_LOG_PRINTFILE ? "true" : "false");

	logger->info(LTRACE, "tuxone.app.log.printConsole=%s", m_TUXONE_APP_LOG_PRINTCONSOLE ? "true" : "false");


}

void AutoConfig::DoPropConstruct()
{
	std::string tmp;

	m_TUXONE_APP_GATE_ADDR = GetPropertyValue("tuxone.app.gate.addr", m_TUXONE_APP_GATE_ADDR);
	m_TUXONE_APP_GATE_ADDR = m_TUXONE_APP_GATE_ADDR == "NONE" ? "" : m_TUXONE_APP_GATE_ADDR;

	m_TUXONE_APP_SERVER_LISTENER_ADDR = GetPropertyValue("tuxone.app.server.listener.addr", m_TUXONE_APP_SERVER_LISTENER_ADDR);
	m_TUXONE_APP_SERVER_LISTENER_ADDR = m_TUXONE_APP_SERVER_LISTENER_ADDR == "NONE" ? "" : m_TUXONE_APP_SERVER_LISTENER_ADDR;

	m_TUXONE_APP_SERVER_CONCURRENTMAX = GetPropertyValue("tuxone.app.server.concurrentMax", m_TUXONE_APP_SERVER_CONCURRENTMAX);

	m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS = GetPropertyValue("tuxone.app.server.maxLongConnections", m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS);

	tmp = GetPropertyValue("tuxone.app.server.threadMode", "MT");
	if(tmp == "MT") m_TUXONE_APP_SERVER_THREADMODE = MT;
	if(tmp == "ST") m_TUXONE_APP_SERVER_THREADMODE = ST;

	m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN = GetPropertyValue("tuxone.app.server.threadPool.threadMin", m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN);

	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX = GetPropertyValue("tuxone.app.server.threadPool.threadMax", m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX);

	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE = GetPropertyValue("tuxone.app.server.threadPool.threadMaxIdle", m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE);

	m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS = GetPropertyValue("tuxone.app.client.cachedConnections", m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS);

	m_TUXONE_APP_HEART_INTERVAL = GetPropertyValue("tuxone.app.heart.interval", m_TUXONE_APP_HEART_INTERVAL);

	m_TUXONE_APP_BUFFER_CACHEDSIZE = GetPropertyValue("tuxone.app.buffer.cachedSize", m_TUXONE_APP_BUFFER_CACHEDSIZE);

	m_TUXONE_APP_SOCKET_RWTIMEOUT = GetPropertyValue("tuxone.app.socket.rwtimeout", m_TUXONE_APP_SOCKET_RWTIMEOUT);

	m_TUXONE_APP_LOG_ENABLE = GetPropertyValue("tuxone.app.log.enable", m_TUXONE_APP_LOG_ENABLE);

	m_TUXONE_APP_LOG_LEVEL = GetPropertyValue("tuxone.app.log.level", m_TUXONE_APP_LOG_LEVEL);

	m_TUXONE_APP_LOG_PRINTWHERE = GetPropertyValue("tuxone.app.log.printWhere", m_TUXONE_APP_LOG_PRINTWHERE);

	m_TUXONE_APP_LOG_PRINTFILE = GetPropertyValue("tuxone.app.log.printFile", m_TUXONE_APP_LOG_PRINTFILE);

	m_TUXONE_APP_LOG_PRINTCONSOLE = GetPropertyValue("tuxone.app.log.printConsole", m_TUXONE_APP_LOG_PRINTCONSOLE);

	m_TUXONE_APP_SERVER_CONCURRENTMAX = m_TUXONE_APP_SERVER_CONCURRENTMAX <= 0 ? 1 : m_TUXONE_APP_SERVER_CONCURRENTMAX;
	m_TUXONE_APP_SERVER_CONCURRENTMAX = m_TUXONE_APP_SERVER_CONCURRENTMAX > 2147483647 ? 2147483647 : m_TUXONE_APP_SERVER_CONCURRENTMAX;

	m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS = m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS < 0 ? 0 : m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS;
	m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS = m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS > 2147483647 ? 2147483647 : m_TUXONE_APP_SERVER_MAXLONGCONNECTIONS;

	m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN = m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN <= 0 ? 1 : m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN;
	m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN = m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN > 2147483647 ? 2147483647 : m_TUXONE_APP_SERVER_THREADPOOL_THREADMIN;

	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX = m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX <= 0 ? 1 : m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX;
	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX = m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX > 2147483647 ? 2147483647 : m_TUXONE_APP_SERVER_THREADPOOL_THREADMAX;

	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE = m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE <= 0 ? 1 : m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE;
	m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE = m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE > 2147483647 ? 2147483647 : m_TUXONE_APP_SERVER_THREADPOOL_THREADMAXIDLE;

	m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS = m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS < 0 ? 0 : m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS;
	m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS = m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS > 2147483647 ? 2147483647 : m_TUXONE_APP_CLIENT_CACHEDCONNECTIONS;

	m_TUXONE_APP_HEART_INTERVAL = m_TUXONE_APP_HEART_INTERVAL <= 0 ? 1 : m_TUXONE_APP_HEART_INTERVAL;
	m_TUXONE_APP_HEART_INTERVAL = m_TUXONE_APP_HEART_INTERVAL > 2147483647 ? 2147483647 : m_TUXONE_APP_HEART_INTERVAL;

	m_TUXONE_APP_BUFFER_CACHEDSIZE = m_TUXONE_APP_BUFFER_CACHEDSIZE < 0 ? 0 : m_TUXONE_APP_BUFFER_CACHEDSIZE;
	m_TUXONE_APP_BUFFER_CACHEDSIZE = m_TUXONE_APP_BUFFER_CACHEDSIZE > 2147483647 ? 2147483647 : m_TUXONE_APP_BUFFER_CACHEDSIZE;

	m_TUXONE_APP_SOCKET_RWTIMEOUT = m_TUXONE_APP_SOCKET_RWTIMEOUT <= 0 ? 1 : m_TUXONE_APP_SOCKET_RWTIMEOUT;
	m_TUXONE_APP_SOCKET_RWTIMEOUT = m_TUXONE_APP_SOCKET_RWTIMEOUT > 2147483647 ? 2147483647 : m_TUXONE_APP_SOCKET_RWTIMEOUT;

	m_TUXONE_APP_LOG_LEVEL = m_TUXONE_APP_LOG_LEVEL < 0 ? 0 : m_TUXONE_APP_LOG_LEVEL;
	m_TUXONE_APP_LOG_LEVEL = m_TUXONE_APP_LOG_LEVEL > 7 ? 7 : m_TUXONE_APP_LOG_LEVEL;


}
